#include "prosetpage.h"
#include "ui_prosetpage.h"

#include <QLineEdit>
#include <QDir>
#include <QFileDialog>
#include <QDebug>

ProSetPage::ProSetPage(QWidget *parent)
    : QWizardPage(parent)
    , ui(new Ui::ProSetPage)
{
    ui->setupUi(this);

    registerField("proPath", ui->lineEditFilePath);
    registerField("proName*", ui->lineEditFileName);    // *号表示该字段是必填字段，会检查是否为空

    connect(ui->lineEditFileName, &QLineEdit::textEdited, this, &ProSetPage::completeChanged);
    connect(ui->lineEditFilePath, &QLineEdit::textEdited, this, &ProSetPage::completeChanged);

    QString curPath = QDir::currentPath();
    ui->lineEditFilePath->setText(curPath);
    ui->lineEditFilePath->setCursorPosition(ui->lineEditFilePath->text().size());   //设置光标

    ui->lineEditFileName->setClearButtonEnabled(true);  // 设置一键清除按钮
    ui->lineEditFilePath->setClearButtonEnabled(true);

}

ProSetPage::~ProSetPage()
{
    delete ui;
}


// 用于确定当前向导页面是否已完成
bool ProSetPage::isComplete() const
{
    if(ui->lineEditFileName->text() == "" || ui->lineEditFilePath->text() == "") {
        // ui->tips->setText("FileName or FilePaht is not empty");
        return false;
    }


    // 判断路径目录是否存在
    QString fileDirectory = ui->lineEditFilePath->text();
    QDir dir(fileDirectory);

    if (!dir.exists()) {
        ui->tips->setText("project directory path is not exists");
        return false;
    }

    // 然后再判断文件名是否相同
    QString absFilePath = dir.absoluteFilePath(ui->lineEditFileName->text());

    QDir targetFilePath(absFilePath);

    if (targetFilePath.exists()) {
        ui->tips->setText("project has exists，change FileName or FilePaht");
            return false;
    }
    ui->tips->setText("");

        // 调用基类的isComplete()方法，确保其他默认行为和状态通知
        return QWizardPage::isComplete();

}


void ProSetPage::on_pushButton_clicked()
{
    QFileDialog fileDialog;
    fileDialog.setFileMode(QFileDialog::Directory); // 表示文件对话框将只显示目录，并且用户只能选择目录
    fileDialog.setWindowTitle("选择导入的文件夹");

    auto path = QDir::currentPath();    // 获取当前工作目录的路径
    fileDialog.setDirectory(path);  // 这个方法设置文件对话框的当前目录，使对话框在打开时显示这个目录的内容。
    fileDialog.setViewMode(QFileDialog::Detail);    // 这一行设置文件对话框的视图模式为详细视图。


    QStringList fileNames;
    if (fileDialog.exec()) {
        fileNames = fileDialog.selectedFiles();
    }

    if (fileNames.length() <= 0) {
        return;
    }

    QString importPath = fileNames.at(0);
    ui->lineEditFilePath->setText(importPath);
}



// void ProSetPage::on_pushButton_clicked()
// {
//     QFileDialog fileDialog;
//     fileDialog.setFileMode(QFileDialog::Directory); // 表示文件对话框将只显示目录，并且用户只能选择目录

//     fileDialog.setWindowTitle("选择导入的文件夹");

//     auto path = QDir::currentPath();
//     fileDialog.setDirectory(path);
//     fileDialog.setViewMode(QFileDialog::Detail);

//     QStringList fileNames;
//     if (fileDialog.exec()) {
//         fileNames = fileDialog.selectedFiles();
//     }

//     if (fileNames.length() <= 0) {
//         return;
//     }

//     QString importPath = fileNames.at(0);
//     ui->lineEditFilePath->setText(importPath);


// }

